/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "TimeDiv.h"

namespace FaceCat{
    Long TimeDiv::getPaintingBackColor(){
        return FCColor_Back;
    }
    
    Long TimeDiv::getPaintingBorderColor(){
        return FCColor_Border;
    }
    
    Long TimeDiv::getPaintingTextColor(){
        return FCColor_Text;
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    
    TimeDiv::TimeDiv(FCCalendar *calendar){
        m_calendar = calendar;
        m_height = 40;
        m_spinHour = 0;
        m_spinMinute = 0;
        m_spinSecond = 0;
        onLoad();
    }
    
    TimeDiv::~TimeDiv(){
        m_calendar = 0;
        m_spinHour = 0;
        m_spinMinute = 0;
        m_spinSecond = 0;
    }
    
    FCCalendar* TimeDiv::getCalendar(){
        return m_calendar;
    }
    
    void TimeDiv::setCalendar(FCCalendar *value){
        m_calendar = value;
    }
    
    int TimeDiv::getHeight(){
        return m_height;
    }
    
    void TimeDiv::setHeight(int value){
        m_height = value;
    }
    
    int TimeDiv::getHour(){
        if (m_spinHour){
            return (int)m_spinHour->getValue();
        }
        else{
            return 0;
        }
    }
    
    void TimeDiv::setHour(int value){
        if (m_spinHour){
            m_spinHour->setValue(value);
        }
    }
    
    int TimeDiv::getMinute(){
        if (m_spinMinute){
            return (int)m_spinMinute->getValue();
        }
        else{
            return 0;
        }
    }
    
    void TimeDiv::setMinute(int value){
        if (m_spinMinute){
            m_spinMinute->setValue(value);
        }
    }
    
    int TimeDiv::getSecond(){
        if (m_spinSecond){
            return (int)m_spinSecond->getValue();
        }
        else{
            return 0;
        }
    }
    
    void TimeDiv::setSecond(int value){
        if (m_spinSecond){
            m_spinSecond->setValue(value);
        }
    }
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    
    void TimeDiv::callEvent(String eventName, Object sender, Object invoke){
        if(eventName == L"onvaluechanged"){
            onSelectedTimeChanged();
        }
    }

    void TimeDiv::onLoad(){
        if (m_calendar){
            if (!m_spinHour){
                m_spinHour = new FCSpin();
                m_spinHour->setMaximum(23);
                m_calendar->addView(m_spinHour);
                m_spinHour->setText(L"0");
                m_spinHour->addEvent((FCEventCallBack*)this, L"onvaluechanged", this);
            }
            if (!m_spinMinute){
                m_spinMinute = new FCSpin();
                m_spinMinute->setMaximum(59);
                m_calendar->addView(m_spinMinute);
                m_spinMinute->setText(L"0");
                m_spinMinute->addEvent((FCEventCallBack*)this, L"onvaluechanged", this);
            }
            if (!m_spinSecond){
                m_spinSecond = new FCSpin();
                m_spinSecond->setMaximum(59);
                m_calendar->addView(m_spinSecond);
                m_spinSecond->setText(L"0");
                m_spinSecond->addEvent((FCEventCallBack*)this, L"onvaluechanged", this);
            }
        }
    }
    
    void TimeDiv::onPaint(FCPaint *paint, const FCRect& clipRect){
        int width = m_calendar->getWidth(), height = m_calendar->getHeight();
        int top = height - m_height;
        FCRect rect ={0, height - m_height, width, height};
        paint->fillRect(getPaintingBackColor(), rect);
        if (m_height > 0){
            Long textColor = getPaintingTextColor();
            FCFont *font = m_calendar->getFont();
            FCSize tSize = paint->textSize(L"时", font, -1);
            FCRect tRect ={0};
            tRect.left = width / 3 - tSize.cx;
            tRect.top = top + m_height / 2 - tSize.cy / 2;
            tRect.right = tRect.left + tSize.cx;
            tRect.bottom = tRect.top + tSize.cy;
            paint->drawText(L"时", textColor, font, tRect, -1);
            tSize = paint->textSize(L"分", font, -1);
            tRect.left = width * 2 / 3 - tSize.cx;
            tRect.top = top + m_height / 2 - tSize.cy / 2;
            tRect.right = tRect.left + tSize.cx;
            tRect.bottom = tRect.top + tSize.cy;
            paint->drawText(L"分", textColor, font, tRect, -1);
            tSize = paint->textSize(L"秒", font, -1);
            tRect.left = width - tSize.cx - 5;
            tRect.top = top + m_height / 2 - tSize.cy / 2;
            tRect.right = tRect.left + tSize.cx;
            tRect.bottom = tRect.top + tSize.cy;
            paint->drawText(L"秒", textColor, font, tRect, -1);
        }
    }
    
    void TimeDiv::onSelectedTimeChanged(){
        if (m_calendar){
            m_calendar->onSelectedTimeChanged();
        }
    }
    
    void TimeDiv::onTimer(){
    }
    
    void TimeDiv::update(){
        if (m_height > 0){
            int width = m_calendar->getWidth(), height = m_calendar->getHeight();
            int top = height - m_height;
            int left = 5;
            if (m_spinHour){
                m_spinHour->setVisible(true);
                FCPoint location ={left, top + m_height / 2 - m_spinHour->getHeight() / 2};
                m_spinHour->setLocation(location);
                m_spinHour->setWidth((width - 15) / 3 - 20);
            }
            if (m_spinMinute){
                m_spinMinute->setVisible(true);
                FCPoint location ={width / 3 + 5, top + m_height / 2 - m_spinMinute->getHeight() / 2};
                m_spinMinute->setLocation(location);
                m_spinMinute->setWidth((width - 15) / 3 - 20);
            }
            if (m_spinSecond){
                m_spinSecond->setVisible(true);
                FCPoint location ={width * 2 / 3 + 5, top + m_height / 2 - m_spinSecond->getHeight() / 2};
                m_spinSecond->setLocation(location);
                m_spinSecond->setWidth((width - 15) / 3 - 20);
                
            }
        }
        else{
            if (m_spinHour){
                m_spinHour->setVisible(false);
            }
            if (m_spinMinute){
                m_spinMinute->setVisible(false);
            }
            if (m_spinSecond){
                m_spinSecond->setVisible(false);
            }
        }
    }
}
